@extends('layouts._app')
@section('content')
    <body>
    <div class="wrap-container">
        <div class="column-content-detail">
            {!! Form::open(['route' => 'blog.gallery.index','class'=>'layui-form','method'=>'get']) !!}
            <div class="layui-form-item">
                <div class="layui-inline tool-btn">
                    <a class="layui-btn layui-btn-small layui-btn-primary" id='email-refresh-btn'
                       href="{{route('blog.gallery.index')}}" data-title='刷新'>
                        <i class="layui-icon">&#x1002;</i>
                    </a>
                    <button class="layui-btn layui-btn-small layui-btn-danger delBtn hidden-xs">
                        <i class="layui-icon">&#xe640;</i>
                    </button>
                </div>
                <div class="layui-inline">
                    {!! Form::search('name',"{$request['name']}",['class'=>'layui-input','placeholder'=>'请输入标题']) !!}
                </div>
                <div class="layui-inline">
                    <select name="is_show" lay-filter="" lay-search>
                        <option value="0">全部</option>
                        <option value="1" @if($request['is_show'] == 1) selected @endif>显示</option>
                        <option value="2" @if($request['is_show'] == 2) selected @endif>隐藏</option>
                    </select>
                </div>
                <div class="layui-inline">
                    <select name="status" lay-filter="" lay-search>
                        <option value="0">全部</option>
                        <option value="1" @if($request['status'] == 1) selected @endif>待审核</option>
                        <option value="2" @if($request['status'] == 2) selected @endif>审核成功</option>
                        <option value="3" @if($request['status'] == 3) selected @endif>审核失败</option>
                    </select>
                </div>
                {!! Form::submit('搜索',['class'=>'layui-btn layui-btn-normal','lay-submit'=>'search']) !!}
                <div class="layui-inline">
                    <select id="category" lay-filter="gallery" lay-search>
                        <option value="0">全部</option>
                    </select>
                </div>
                <div class="layui-inline" style="float: right;">
                    <select id="change_category" lay-filter="category" lay-search data-id="galleries">
                        <option selected>下载</option>
                        <option>.txt</option>
                        <option>.xlsx</option>
                    </select>
                </div>
            </div>
            {!! Form::close() !!}
            @include('layouts._flash')
            <div class="layui-form" id="table-list">
                <table class="layui-table">
                    <thead>
                    <tr>
                        <th><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>
                        <th>ID</th>
                        <th>用户ID</th>
                        <th>用户名</th>
                        <th>标题</th>
                        <th>封面图</th>
                        <th>分类</th>
                        <th>浏览数</th>
                        <th>评论数</th>
                        <th>分享</th>
                        <th>喜欢</th>
                        <th>踩</th>
                        <th>是否置顶</th>
                        <th>是否评论</th>
                        <th>状态</th>
                        <th>失败原因</th>
                        <th>查看</th>
                        <th>创建时间</th>
                        <th>当前状态</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($gallery as $value)
                        <tr>
                            <td><input type="checkbox" name="del" lay-skin="primary" data-id="{{$value->id}}"></td>
                            <td>{{$value->id}}</td>
                            <td>{{$value->user_id}}</td>
                            <td title="{{$value->user->name ?? ''}}">{{$value->user->name ?? ''}}</td>
                            <td title="{{$value->title}}">{{sub($value->title,20)}}</td>
                            <td title="{{$value->title}}">
                                @if(isset($value->file->url))
                                    <img src="{{$value->file->url}}" style="width: 100px;" alt="{{$value->title}}">
                                @endif
                            </td>
                            <td title="{{$value->category->name ?? ''}}">{{sub($value->category->name ?? '',20)}}</td>
                            <td title="{{$value->browse}}">{{sub($value->browse,20)}}</td>
                            <td title="{{$value->comment}}">{{sub($value->comment,20)}}</td>
                            <td title="{{$value->share}}">{{sub($value->share,20)}}</td>
                            <td title="{{$value->like}}">{{sub($value->like,20)}}</td>
                            <td title="{{$value->step}}">{{sub($value->step,20)}}</td>
                            <td title="{{$value->root == 1 ? '否' : '是'}}">{{$value->root == 1 ? '否' : '是'}}</td>
                            <td title="{{$value->is == 1 ? '允许':'禁止'}}">{{$value->is == 1 ? '允许':'禁止'}}</td>
                            <td title="{{$value->status == 1 ? '待审核' : ($value->status == 2 ? '审核成功':'审核失败')}}">{{$value->status == 1 ? '待审核' : ($value->status == 2 ? '审核成功':'审核失败')}}</td>
                            <td title="{{$value->fail ?: '无'}}">{{sub($value->fail ?: '无',20)}}</td>
                            <td title="查看详情">
                                <a class="layui-btn layui-btn-mini layui-btn-normal"
                                   href="{{route('blog.gallery.show',$value->id)}}">查看详情</a></td>
                            <td title="{{$value->created_at}}">{{sub($value->created_at,20)}}</td>
                            <td title="@if($value->is_show == 1) 显示 @else 隐藏 @endif">
                                <a class="layui-btn layui-btn-mini layui-btn-normal"
                                   href="{{route('blog.gallery.status',$value->id)}}"
                                   data-method="delete"
                                   data-token="{{csrf_token()}}"
                                   data-confirm="@if($value->is_show == 1) 确认禁用? @else 确认启用? @endif">
                                    @if($value->is_show == 1) 显示 @else 隐藏 @endif
                                </a>
                            </td>
                            <td>
                                <div class="layui-inline" data-id="{{$value->id}}">
                                    @if($value->status == 1)
                                        <a class="layui-btn layui-btn-small layui-btn-normal check"
                                           href="javascript:void(0);" title="通过">
                                            <i class="fa fa-check"></i></a>
                                        <a class="layui-btn layui-btn-small layui-btn-warm times"
                                           href="javascript:void(0);" title="拒绝">
                                            <i class="fa fa-times"></i></a>
                                    @endif
                                    <a class="layui-btn layui-btn-small layui-btn-danger"
                                       href="{{route('blog.gallery.destroy',$value->id)}}"
                                       data-method="delete" title="文章删除"
                                       data-token="{{csrf_token()}}" data-confirm="确认删除吗?">
                                        <i class="layui-icon">&#xe640;</i></a>
                                </div>
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
                <!--tp分页-->
                <div class="page-wrap">
                    <p style="float: left;">注 : 共 {{$gallery->total()}} 条记录</p>
                    {{ $gallery->links() }}
                </div>
            </div>
        </div>
    </div>
    <div style="display: none;" id="changeArea" class="change-pas-box">
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">拒绝理由</label>
            <div class="layui-input-inline">
                {!!  Form::textarea('content','请输入内容！！！',['class'=>'layui-textarea','id'=>'content']) !!}
            </div>
        </div>
    </div>
    <script src="{{asset('static/admin/layui/layui.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('static/admin/js/common.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/jquery.min.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/destroy.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/common.js')}}" type="text/javascript" charset="utf-8"></script>
    <script>

        layui.use(['jquery', 'dialog'], function () {
            var $ = layui.jquery;
            var dialog = layui.dialog;
            //顶部批量删除
            $('.delBtn').click(function () {
                var ids = [];
                $("input[name='del']:checked").each(function (i) {
                    ids[i] = $(this).data('id');
                });
                if (ids.length == 0) {
                    layer.msg('请选择要删除的', {icon: 5, time: 3000});
                    return false;
                }
                dialog.confirm({
                    message: '您确定要删除选中项',
                    success: function () {
                        $.ajax({
                            type: 'POST',
                            url: '{{route('blog.gallery.delete')}}',
                            data: {id: ids, filter: true},
                            dataType: "json",
                            beforeSend: function () {
                                layer.load(1, {
                                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                                });
                            },
                            complete: function (XHR) {
                                XHR = null;
                            },
                            error: function (xhr) {//获取ajax的错误信息
                                layer.alert(xhr.responseText, "Failed");
                            },
                            success: function (res) {
                                layer.closeAll();
                                if (res.code == 403) {
                                    layer.msg(res.msg, {icon: 4, time: 3000})
                                    return false;
                                }
                                if (res.code != 200) {
                                    layer.msg(res.msg, {icon: 5, time: 3000})
                                    return false;
                                }
                                layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                                    window.location.reload();
                                });
                            }
                        })
                    },
                    cancel: function () {
                        return false;
                    }
                })
                return false;

            }).mouseenter(function () {

                dialog.tips('批量删除', '.delBtn');

            })

            //通过审核
            $('.check').click(function () {
                var id = $(this).parents('div').data('id');
                dialog.confirm({
                    message: '通过审核？',
                    success: function () {
                        $.ajax({
                            type: 'PATCH',
                            url: '{{route('blog.gallery.examine')}}',
                            data: {status: 2, id: id},
                            dataType: "json",
                            beforeSend: function () {
                                layer.load(1, {
                                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                                });
                            },
                            complete: function (XHR) {
                                XHR = null;
                            },
                            error: function (xhr) {//获取ajax的错误信息
                                layer.alert(xhr.responseText, "Failed", function () {
                                    layer.closeAll();
                                });
                            },
                            success: function (res) {
                                layer.closeAll();
                                if (res.code != 200) {
                                    layer.msg(res.msg, {icon: 5, time: 3000})
                                    return false;
                                }
                                if (res.code == 403) {
                                    layer.msg(res.msg, {icon: 4, time: 3000})
                                    return false;
                                }
                                layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                                    window.location.reload();
                                });
                            }
                        })
                    },
                    cancel: function () {
                        return false;
                    }
                })
                return false;

            })

            //拒绝
            $('.times').click(function () {
                var id = $(this).parents('div').data('id');
                var fail = '';
                layer.open({
                    type: 1,
                    btnAlign: 'c',
                    area: ['600px', '400px'],
                    title: '审核',
                    content: $("#changeArea"),
                    btn: ['提交', '取消'],
                    btn1: function () {
                        var title = $('#content').val();
                        $.ajax({
                            type: 'PATCH',
                            url: '{{route('blog.gallery.examine')}}',
                            data: {status: 3, id: id, fail: title},
                            dataType: "json",
                            beforeSend: function () {
                                layer.load(1, {
                                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                                });
                            },
                            complete: function (XHR) {
                                XHR = null;
                            },
                            error: function (xhr) {//获取ajax的错误信息
                                layer.alert(xhr.responseText, "Failed", function () {
                                    layer.closeAll();
                                });
                            },
                            success: function (res) {
                                layer.closeAll();
                                if (res.code != 200) {
                                    layer.msg(res.msg, {icon: 5, time: 3000})
                                    return false;
                                }
                                if (res.code == 403) {
                                    layer.msg(res.msg, {icon: 4, time: 3000})
                                    return false;
                                }
                                layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                                    window.location.reload();
                                });
                            }
                        })
                    },
                })
            })

            layui.use(['form'], function () {
                var form = layui.form();
                $.ajax({
                    type: 'get',
                    url: '{{route('blog.gallery.category')}}',
                    dataType: "json",
                    beforeSend: function () {
                        layer.load(1, {
                            shade: [0.1, '#333333'] //0.1透明度的白色背景
                        });
                    },
                    complete: function (XHR) {
                        XHR = null;
                    },
                    error: function (xhr) {//获取ajax的错误信息
                        layer.alert(xhr.responseText, "Failed", function () {
                            layer.closeAll();
                        });
                    },
                    success: function (response) {
                        layer.closeAll();
                        if (response.code != 200) {
                            layer.msg(res.msg, {icon: 5, time: 3000})
                            return false;
                        }
                        if (response.code == 403) {
                            layer.msg(response.msg, {icon: 4, time: 3000})
                            return false;
                        }
                        var pro = response.data;
                        var proHtml = '<option value="0">全部</option>';
                        for (var i = 0; i < pro.length; i++) {
                            if (pro[i].id == '{{$request['category']}}') proHtml += '<option value="' + pro[i].id + '" selected>' + pro[i].name + '</option>';
                            else proHtml += '<option value="' + pro[i].id + '">' + pro[i].name + '</option>';
                        }
                        //初始化省数据
                        $("select[id=category]").html(proHtml);
                        form.render();
                    }
                })
                form.on('select(gallery)', function (e) {
                    window.location = "{{route('blog.gallery.index')}}?name={{$request['name']}}&status={{$request['status']}}&is_show={{$request['is_show']}}&category=" + e.value + "&page={{$request['page']}}"
                });
                form.render();
                form.on('select(category)', function (e) {
                    if (e.value == '下载') {
                        return false;
                    }
                    /**
                     * 写入文件下载
                     * @param id
                     */
                    var table = $("#change_category").data('id');
                    var ext = e.value;
                    dialog.confirm({
                        message: '这可能会耗费很长时间！是否继续',
                        success: function () {
                            window.open('{{route('TextFile')}}' + '?table=' + table + '&ext=' + ext, '_parent');
                            window.location.reload();
                        },
                    });
                });
                form.render();
            });

        })
    </script>
    </body>
@endsection
